package com.example.zzm.yuejian03.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzm.yuejian03.Bean.MasterBean;
import com.example.zzm.yuejian03.Bean.collectBean;
import com.example.zzm.yuejian03.Listener.TListener;
import com.example.zzm.yuejian03.Model.CollectclubModel;
import com.example.zzm.yuejian03.Model.MasterdetailsModel;
import com.example.zzm.yuejian03.R;
import com.squareup.picasso.Picasso;

public class MasterdetailsActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView11,textView49,textView50,textView52;
    private ImageView imageView14;
    private Button button15,button16,button17;
    private int resid;
    private Context context;
    private SharedPreferences sp;//简单存储
    private int userid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_masterdetails);
        //System.out.println("----查看俱乐部详情");
        resid  = getIntent().getIntExtra("club_id",1);//获取传递的资源id
        sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        readSP();//读取sessionid
        initViews();
        initEvent();
        MasterdetailsModel ActivdetailsModel = new MasterdetailsModel();
        ActivdetailsModel.getMasterdetailss(getIntent().getIntExtra("mid",1),listener);
        CollectclubModel model=new CollectclubModel();//实例化对象
        model.islike(resid,userid,tlistener);//判断是否收藏
        //resid  = getIntent().getIntExtra("club_id",1);
    }
    private void readSP() {
        userid = sp.getInt("user_id",1);
    }
    TListener<MasterBean> listener = new TListener<MasterBean>() {
        @Override
        public void onResponse(MasterBean picture) {
            textView49.setText(picture.getName());
            textView50.setText(picture.getIntro());
            textView52.setText(picture.getClubname());
            Picasso.with(MasterdetailsActivity.this)
                    .load("http://10.0.2.2/yuejian10/public/static/asstes/images/" + picture.getPic())
                    .placeholder(R.mipmap.ic_launcher)
                    // .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(imageView14);
        }
        @Override
        public void onFail(String msg) {

        }
    };

    private void initViews(){
        textView49 = (TextView)findViewById(R.id.textView49);//mingc
        textView50 = (TextView)findViewById(R.id.textView50);//jieshao
        textView52= (TextView)findViewById(R.id.textView52);//jiaolian
        //imageView=(ImageView)findViewById(R.id.imageView);
        imageView14 = (ImageView)findViewById(R.id.imageView14);
        textView11 = (TextView) findViewById(R.id.textView11);//fanhui
        button15 = (Button)findViewById(R.id.button15);
        button16 = (Button)findViewById(R.id.button16);
        button17 = (Button)findViewById(R.id.button17);
        // editText = (EditText)findViewById(R.id.editText);
//        button10 = (Button)findViewById(R.id.button10);
    }

    private void initEvent(){
        textView11.setOnClickListener(this);
        button15.setOnClickListener(this);
        button16.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MasterdetailsActivity.this,ClassListActivity.class);
                startActivityForResult(intent,1);
                finish();
            }
        });
        button17.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MasterdetailsActivity.this,AnswerActivity.class);
                startActivityForResult(intent,1);
                finish();
            }
        });
    }
    TListener<collectBean> ttlistener=new TListener<collectBean>() {
        @Override
        public void onResponse(collectBean collectBean) {
            if (collectBean.getError().equals("0")){
                Toast.makeText(MasterdetailsActivity.this,"收藏成功！", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MasterdetailsActivity.this,"收藏失败！", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
        }
    };
    TListener<collectBean> tlistener=new TListener<collectBean>() {
        @Override
        public void onResponse(collectBean collectBean) {
            if (collectBean.getError().equals("0")){
                button15.setText("取消收藏");
            }else {
                button15.setText("收藏");
            }
        }
        @Override
        public void onFail(String msg) {
        }
    };
    TListener<collectBean> tttlistener=new TListener<collectBean>() {
        @Override
        public void onResponse(collectBean collectBean) {
            if (collectBean.getError().equals("0")){
                Toast.makeText(MasterdetailsActivity.this,"取消收藏成功", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MasterdetailsActivity.this,"取消收藏失败", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
        }
    };
    @Override
    public void onClick(View v) {
        CollectclubModel model=new CollectclubModel();//实例化对象
        switch (v.getId()){
            case R.id.textView11:
                finish();
                break;
            case R.id.button15:
                if(button15.getText().equals("收藏"))//如果已收藏，则调用取消收藏
                {
                    model.Like(resid,userid,ttlistener);
                    button15.setText("取消收藏");
                }
                else//如果未收藏，则调用收藏
                {
                    model.unLike(resid,userid,tttlistener);
                    button15.setText("收藏");
                }
                break;
        }
    }
}
